 clear all
set more off
capture log close
set scheme s2color


use "cp_compfinal.dta", clear
keep if year >= 1950 & year <= 2006

* Key variables for analysis
local key_vars max_POLDIS_binary max_ECDIS_binary exclpop mi_in mi_supporter actmilcabshare

* Control variables  
local controls polity polity_squared cultural_dif mean_SEPX mean_PROT mean_REB ln_gdpcap lfirstpi

* Generate and export summary statistics table
preserve
postfile sumstats str30 variable obs mean sd min max str50 source using "temp_sumstats.dta", replace

foreach var in `key_vars' `controls' {
    quietly summarize `var'
    local varlabel : variable label `var'
    
    * Assign data sources
    local source = ""
    if inlist("`var'", "max_POLDIS_binary", "max_ECDIS_binary", "cultural_dif", "mean_SEPX", "mean_PROT", "mean_REB") {
        local source = "AMAR (Birnir et al., 2015)"
    }
    else if "`var'" == "exclpop" {
        local source = "EPR v3 (Wimmer et al., 2012)"  
    }
    else if inlist("`var'", "mi_in", "mi_supporter") {
        local source = "PRM (Croissant et al., 2019)"
    }
    else if "`var'" == "actmilcabshare" {
        local source = "MPG (White, 2021)"
    }
    else if inlist("`var'", "polity", "polity_squared") {
        local source = "Polity Project"
    }
    else if "`var'" == "lfirstpi" {
        local source = "CPDS (Armingeon et al., 2021)"
    }
    else if "`var'" == "ln_gdpcap" {
        local source = "World Bank"
    }
    
    post sumstats ("`varlabel'") (r(N)) (r(mean)) (r(sd)) (r(min)) (r(max)) ("`source'")
}

postclose sumstats
restore

* Load and format summary statistics
use "temp_sumstats.dta", clear
format obs %9.0f
format mean sd min max %9.3f
export delimited using "Table1_Summary_Statistics.csv", replace
save "Table1_Summary_Statistics.dta", replace

* Display summary table
list variable obs mean sd min max source, clean noobs